package com.alcj.controller;

import com.alcj.common.constant.SysConstant;
import com.alcj.common.framework.PageController;
import com.alcj.common.utils.CmnUtil;
import com.alcj.model.Cost;
import com.alcj.model.ReviewDto;
import com.alcj.model.Station;
import com.alcj.service.CostService;
import com.alcj.service.ReviewService;
import com.alcj.service.StationService;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author :wangpf
 * @version :v1.0
 * @Package :com.alcj.controller
 * @Description :${TODO}(检测站查询)
 * @date :2017/9/25 0005 上午 11:10
 */
@Controller
@RequestMapping("/review")
public class ReviewController extends PageController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    StationService stationService;

    @Autowired
    CostService costService;

    @Autowired
    ReviewService reviewService;

    @RequestMapping("/getReviewList")
    public String getReviewList(HttpServletRequest request, Model model) {

        try{

            HttpSession session = request.getSession();

            String  stationId = request.getParameter("stationIdForReview");

            // 评价条数
            Integer reviewCount = 0;
            // 总分数
            Integer sumGrade = 0;
            // 差评数
            Integer positiveCount = 0;
            // 中评数
            Integer moderateCount = 0;
            // 好评数
            Integer negativeCount = 0;
            // 差评所占百分比
            Double positivePercent = 0.0;
            // 中评所占百分比
            Double moderatePercent = 0.0;
            // 好评所占百分比
            Double negativePercent = 0.0;

            ReviewDto review = new ReviewDto();
            review.setStationid(stationId);

            // 根据检测站ID获取该检测站的客户评价
            List<ReviewDto> reviewDtoList = reviewService.selectReviewDtoListByStationId(stationId);
            reviewCount = reviewDtoList.size();

            for (ReviewDto reviewDto : reviewDtoList){

                sumGrade += reviewDto.getGrade();

                if (reviewDto.getGrade() == 0 || reviewDto.getGrade() == 1){
                    positiveCount += 1;
                }else if (reviewDto.getGrade() == 2 || reviewDto.getGrade() ==3){
                    moderateCount += 1;
                }else{
                    negativeCount += 1;
                }
            }

            // 总体评价分数
            Double reviewGrade = 0.0;
            if (reviewCount != 0){
                reviewGrade = Math.round(Double.parseDouble(sumGrade.toString())/Double.parseDouble(reviewCount.toString()) * 10) * 0.1d;

                // 差评所占百分比
                positivePercent = Math.round(Double.parseDouble(positiveCount.toString())/Double.parseDouble(reviewCount.toString()) * 100) * 1d;
                // 中评所占百分比
                moderatePercent = Math.round(Double.parseDouble(moderateCount.toString())/Double.parseDouble(reviewCount.toString()) * 100) * 1d;
                // 好评所占百分比
                negativePercent = Math.round(Double.parseDouble(negativeCount.toString())/Double.parseDouble(reviewCount.toString()) * 100) * 1d;

            }
            // 总体评价分数所占百分比
            Double gradePercent = Math.round(reviewGrade/5.0 * 100) * 1d;

            // 服务评价列表
            model.addAttribute("stationIdForReview",stationId);
            // 服务评价列表
            model.addAttribute("reviewDtoList",reviewDtoList);
            // 服务评价条数
            model.addAttribute("reviewCount",reviewCount);
            // 差评数
            model.addAttribute("positiveCount",positiveCount);
            // 中评数
            model.addAttribute("moderateCount",moderateCount);
            // 好评数
            model.addAttribute("negativeCount",negativeCount);
            // 总体评价分数
            model.addAttribute("reviewGrade",reviewGrade);
            // 所占百分比
            model.addAttribute("gradePercent",gradePercent);
            // 差评数所占百分比
            model.addAttribute("positivePercent",positivePercent);
            // 中评数所占百分比
            model.addAttribute("moderatePercent",moderatePercent);
            // 好评数所占百分比
            model.addAttribute("negativePercent",negativePercent);

            // 获取页数
            String currentPage = request.getParameter("currentPage");
            // 设置页数
            if (CmnUtil.isEmpty(currentPage)) {
                currentPage = "1";
            }
            super.setCurrentPage(Integer.parseInt(currentPage));

            super.setMaxSize(6);
            // 设置总件数数
           // int total = reviewService.getReviewCount(stationId);
            super.giveTotalNum(reviewCount);
            if(reviewCount > 0){
                // 设置分页参数
                super.preparePageParams(review);
                // 获取用户评价列表
                List<ReviewDto> reviewList = reviewService.getReviewListForPage(review);

                model.addAttribute("reviewList", reviewList);
            }
            model.addAttribute("j_page", getPage());

        }
        catch(Exception e){

            // 异常报错
            logger.error("获取首页信息异常 : {}", e.getMessage());
            logger.error("异常详细信息 :",e);
            return SysConstant.ERROR_PAGE_500;

        }
        return "review";
    }

}
